//
//  YHCardViewController.m
//  YunHaiManage
//
//  Created by zhangbolin on 2021/12/17.
//

#import "YHCardViewController.h"
#import "YHCardCollectionViewFlowLayout.h"
#import "YHTimeCell.h"
#import "NSDate+FSCalendar.h"

@interface YHCardViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, copy) NSArray *mouthArray;
@property (nonatomic, copy) NSArray *dayArray;
@property (nonatomic, strong) NSDate *nowDate;

@end

@implementation YHCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"八卦罗盘时钟";
    [self.view addSubview:self.collectionView];
    self.mouthArray = @[];
    self.nowDate = [NSDate date];
    // 每隔一秒调用一次update方法
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(update) userInfo:nil repeats:YES];
}

- (void)update {
    [self.collectionView reloadData];
//    for (NSInteger i = 0; i < 4; i ++) {
//        NSInteger count = [self.collectionView numberOfItemsInSection:i];
//        CGFloat angle = 2 * M_PI / count;
//        for (NSInteger index = 0; index < count; index ++) {
//            UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:i]];
//            cell.layer.anchorPoint = CGPointMake(1, 1);
//            // 创建一个基础动画
//            CABasicAnimation *animation = [CABasicAnimation new];
//            // 设置动画要改变的属性
//            animation.keyPath = @"transform.rotation.z";
//            // 动画的最终属性的值（转7.5圈）
//            animation.toValue = @(M_PI*10);
//            // 动画的播放时间
//            animation.duration = 3;
//            // 动画效果慢进慢出
//            animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//            // 解决动画结束后回到原始状态的问题
//            animation.removedOnCompletion = NO;
//            animation.fillMode = kCAFillModeForwards;
//            // 将动画添加到视图的layer上
//            [cell.layer addAnimation:animation forKey:@"rotation"];
//        }
//    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.collectionView.frame = self.view.bounds;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 6;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return [self.nowDate getMonth].count;
    } else if (section == 2) {
        return [self.nowDate totalDaysInMonth];
    } else if (section == 3) {
        return 24;
    } else if (section == 4 || section == 5) {
        return 60;
    }
    return 10 * section;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YHTimeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YHTimeCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell loadDataWithTitle:[NSString stringWithFormat:@"%ld年", [self.nowDate dateYear]]];
    } else if (indexPath.section == 1) {
        [cell loadDataWithTitle:[self.nowDate getMonth][indexPath.row]];
    } else if (indexPath.section == 2) {
        [cell loadDataWithTitle:[NSString stringWithFormat:@"%ld号", indexPath.row + 1]];
    } else if (indexPath.section == 3) {
        [cell loadDataWithTitle:[NSString stringWithFormat:@"%ld时", indexPath.row]];
    } else if (indexPath.section == 4) {
        [cell loadDataWithTitle:[NSString stringWithFormat:@"%ld分", indexPath.row]];
    } else if (indexPath.section == 5) {
        [cell loadDataWithTitle:[NSString stringWithFormat:@"%ld秒", indexPath.row]];
    }
    return cell;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        YHCardCollectionViewFlowLayout *layout = [[YHCardCollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = NO;
        _collectionView.decelerationRate = 0.993;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _collectionView.showsVerticalScrollIndicator   = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [_collectionView registerClass:[YHTimeCell class] forCellWithReuseIdentifier:@"YHTimeCell"];
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _collectionView;
}

@end
